! Module necessary to Grell Cumulus param.
! Scratch variables - module 1

MODULE mem_scratch1_grell

  !TYPE scratch1_grell_vars

     !srf- feb-05-2002 : Variables for cumulus transport scheme
     !                   adapted in july-15-2002 for 5.x version
     !
     !ngrids_cp = numero de grades onde a parametrizacao de cumulus e' usada
     !
     implicit none

     logical :: scratch1_grell_alloc=.false.
     integer :: iruncon = 0

    !4d dependence (mgmxp,mgmyp,maxiens,ngrids_cp)
     INTEGER, ALLOCATABLE, DIMENSION(:,:,:,:) ::  &
          ierr4d,                             & ! ierr4d=0 there is convection
          jmin4d,                             & ! ETL
          kdet4d,                             & ! Detrainemnt level 
          k224d,                              & ! 
          kbcon4d,                            & ! Cloud base
          ktop4d,                             & ! Cloud top
          kstabi4d,                           & ! Cloud base
          kstabm4d,                           & ! Cloud top
          kpbl4d                                ! PBL height

     REAL, ALLOCATABLE, DIMENSION(:,:,:,:) :: &
          xmb4d,                              & ! updraft mass flux
          edt4d                                 ! MFLX_DOWN/MFLX_UP

 
     !5d dependece  (mgmzp,mgmxp,mgmyp,maxiens,ngrids_cp)
     REAL, ALLOCATABLE, DIMENSION(:,:,:,:,:) ::   &
          enup5d,                             & ! entrain up   rate
          endn5d,                             & ! entrain down rate
          deup5d,                             & ! detrain up   rate
          dedn5d,                             & ! detrain down rate
           zup5d,                             & ! norm mass flux up
           zdn5d,                             & ! norm mass flux down
          zcup5d,                             & ! z level
          pcup5d,                             & !p level
          prup5d,                             &
         clwup5d,                             &
           tup5d
  !END TYPE scratch1_grell_vars

CONTAINS

  SUBROUTINE alloc_scratch1_grell !(scratch1_grell)

    USE mem_grell_param, ONLY : mgmxp,  & ! INTENT(IN)
         mgmyp,                         & ! INTENT(IN)
         mgmzp,                         & ! INTENT(IN)
         maxiens,                       & ! INTENT(IN)
         ngrids_cp                        ! INTENT(IN)

    IMPLICIT NONE

    if(scratch1_grell_alloc) then
       print *,'ERROR: ascratch1_grell already allocated'
       print *,'Routine: alloc_scratch1_grell File: mem_scratch1_grell.f90'
       print *,'Dir: .../shared/tools/brams20/src/rams/5.04/modules'
       stop
    end if



    !TYPE (scratch1_grell_vars) :: scratch1_grell

    ALLOCATE (  ierr4d(mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (  jmin4d(mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (  kdet4d(mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (   k224d(mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE ( kbcon4d(mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (  ktop4d(mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (kstabi4d(mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (kstabm4d(mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (  kpbl4d(mgmxp, mgmyp, maxiens, ngrids_cp))

    ALLOCATE (xmb4d(mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (edt4d(mgmxp, mgmyp, maxiens, ngrids_cp))


    ALLOCATE (enup5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (endn5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (deup5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (dedn5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE ( zup5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE ( zdn5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (zcup5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (pcup5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE (prup5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE(clwup5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))
    ALLOCATE ( tup5d(mgmzp, mgmxp, mgmyp, maxiens, ngrids_cp))

    scratch1_grell_alloc=.true.

  END SUBROUTINE alloc_scratch1_grell

  SUBROUTINE dealloc_scratch1_grell !(scratch1_grell)

    IMPLICIT NONE
    !TYPE (scratch1_grell_vars) :: scratch1_grell

    DEALLOCATE (ierr4d)
    DEALLOCATE (jmin4d)
    DEALLOCATE (kdet4d)
    DEALLOCATE (k224d)
    DEALLOCATE (kbcon4d)
    DEALLOCATE (ktop4d)
    DEALLOCATE (kstabi4d)
    DEALLOCATE (kstabm4d)
    DEALLOCATE (kpbl4d)

    DEALLOCATE (xmb4d)
    DEALLOCATE (edt4d)

    DEALLOCATE (zcup5d)
    DEALLOCATE (pcup5d)

    DEALLOCATE (enup5d)
    DEALLOCATE (endn5d)
    DEALLOCATE (deup5d)
    DEALLOCATE (dedn5d)
    DEALLOCATE (zup5d)
    DEALLOCATE (zdn5d)

    DEALLOCATE ( prup5d)
    DEALLOCATE (clwup5d)
    DEALLOCATE (  tup5d)

  END SUBROUTINE dealloc_scratch1_grell

!!$  SUBROUTINE filltab_scratch1_grell()
!!$
!!$    USE var_tables
!!$
!!$    IMPLICIT NONE
!!$
!!$    ! Can't think of anything to do here...
!!$
!!$    RETURN
!!$  END SUBROUTINE filltab_scratch1_grell

 subroutine zero_scratch1_grell()

   implicit none

    zcup5d=0.
    pcup5d=0.
    prup5d=0.
    clwup5d=0.
    tup5d=0.

  end subroutine zero_scratch1_grell




END MODULE mem_scratch1_grell
